from django.core.management.base import BaseCommand, CommandError
from optparse import make_option

from linkcheck.utils import check
from pidman.linkcheck_config import linklists, RECHECK_AFTER_HOURS, MAX_CHECKS_PER_RUN

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--recheck-after-hours', type='int',
            help='Specifies the length of time in hours (int) until links are rechecked. Defaults to linkcheck_config setting'),
        make_option('--limit', type='int',
            help='Specifies the maximum number (int) of links to be checked. Defaults to linkcheck_config setting.  Value less than 1 will check all'),
    )
    help = 'Check and record link status'
    args = '[appname ...]'

    def execute(self, *args, **options):
        if options['recheck_after_hours']:
            recheck = options['recheck_after_hours']
        else:
            recheck = RECHECK_AFTER_HOURS

        if options['limit']:
            limit = options['limit']
        else:
            limit = MAX_CHECKS_PER_RUN
        return check(recheck, limit)